package cn.wolfcode.util;

import cn.wolfcode.anno.CustomJob;
import cn.wolfcode.constant.JobStrategy;
import com.dangdang.ddframe.job.api.ElasticJob;
import com.dangdang.ddframe.job.config.JobCoreConfiguration;
import com.dangdang.ddframe.job.config.JobTypeConfiguration;
import com.dangdang.ddframe.job.config.dataflow.DataflowJobConfiguration;
import com.dangdang.ddframe.job.config.simple.SimpleJobConfiguration;
import com.dangdang.ddframe.job.lite.config.LiteJobConfiguration;
import com.dangdang.ddframe.job.lite.spring.api.SpringJobScheduler;
import com.dangdang.ddframe.job.reg.base.CoordinatorRegistryCenter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.Objects;

/**
 * 一切elasticjob的父类
 */
public abstract class BaseElasticJob implements ElasticJob {

    @Autowired
    private CoordinatorRegistryCenter registryCenter;

    //@Bean(initMethod = "init")
    @PostConstruct
    public void initSimpleElasticJob(){

        // 获取注册信息
        CustomJob customJob = this.getClass().getAnnotation(CustomJob.class);
        if (Objects.isNull(customJob)) {
            throw new RuntimeException("need @CustomJob on job");
        }
        new SpringJobScheduler(this,registryCenter,
                createJobConfiguration(
                    this.getClass(),
                    customJob.cron(),
                    customJob.shardingTotalCount(),
                    customJob.shardingItemParameters(),
                    customJob.jobStrategy())
                ).init();
    }

    public LiteJobConfiguration createJobConfiguration(Class<? extends BaseElasticJob> jobClass,
                                                       String cron,
                                                       int shardingTotalCount,
                                                       String shardingItemParameters,
                                                       JobStrategy jobStrategy) {
        // 定义作业核心配置
        JobCoreConfiguration.Builder jobCoreConfigurationBuilder = JobCoreConfiguration.newBuilder(jobClass.getSimpleName(), cron, shardingTotalCount);
        if(!StringUtils.isEmpty(shardingItemParameters)){
            jobCoreConfigurationBuilder.shardingItemParameters(shardingItemParameters);
        }

        // 自定义job类型
        JobTypeConfiguration jobTypeConfiguration = null;
        switch (jobStrategy) {
            // 定义SIMPLE类型配置
            case SIMPLE: jobTypeConfiguration = new SimpleJobConfiguration(jobCoreConfigurationBuilder.build(), this.getClass().getCanonicalName());break;
            // 定义DATAFLOW类型配置
            case DATAFLOW: jobTypeConfiguration = new DataflowJobConfiguration(jobCoreConfigurationBuilder.build(),jobClass.getCanonicalName(),true);
        }

        // 定义Lite作业根配置
        LiteJobConfiguration simpleJobRootConfig = LiteJobConfiguration.newBuilder(jobTypeConfiguration).overwrite(true).build();
        return simpleJobRootConfig;
    }
}